import os,sys,math,time
from math import ceil
from operator import mod
from pathlib import Path
import cv2
import numpy as np

try:
    media_file = sys.argv[1]
except:
    media_file = 'test.jpg'

medianame = media_file.split('/')[-1]
classNames = ['Motor','Mount']
colors = ['green','red']
labelFile = ''.join([media_file,'.label'])


window_position_x,window_position_y = 1,1
window_width,window_height = 640,480
ptStart_col,ptEnd_col = 0,0
ptStart_row,ptEnd_row = 0,0
positionRow,positionCol = 0,0
ALPHA = 50
pressed_key = []

media = cv2.imread(media_file)
b_channel,g_channel,r_channel = cv2.split(media)
alpha_channel = np.ones(b_channel.shape,dtype=b_channel.dtype) * 255
alpha_channel[:,:int(b_channel.shape[0] / 2)] = ALPHA
img_BGRA = cv2.merge((b_channel,g_channel,r_channel,alpha_channel))

title = ''.join([medianame])
cv2.namedWindow(title, 0)
cv2.moveWindow(title,window_position_x,window_position_y)
cv2.resizeWindow(title, window_width, window_height)

    
def draw_box(image,left,top,right,bottom,color):
    pointLeftTop = (left,top)
    pointRightBottom = (right,bottom)
    width_box = abs(right - left)
    height_box = abs(top - bottom)
    thickness = 2
    lineType = 8
    if color == 'red':
        color = (0,0,255)
    elif color == 'blue':
        color = (255,0,0)
    elif color == 'green':
        color = (0,255,0)
    alpha = 0.4
    new_image = cv2.rectangle(image,pointLeftTop,pointRightBottom,color,thickness,lineType)

    return new_image
    
def draw_tag(img,tag,position_row_tag,position_col_tag):
    tag_position = (position_col_tag,position_row_tag)
    tag_font = cv2.FONT_HERSHEY_SIMPLEX
    tag_bold = 2
    font_size = 1
    tag_color = (209,80,0,255)
    try:
        new_image = cv2.putText(img,tag,tag_position,tag_font,font_size,tag_color,tag_bold)
    except:
        print("draw tag failed.")
    return new_image

def draw_line(image,position,ROW_or_COL,REF_or_TAG):
    global ptStart_col
    global ptStart_row
    global ptEnd_col
    global ptEnd_row

    shape = image.shape
    height = shape[0]
    width = shape[1]

    if ROW_or_COL == 0:
        ptStart_col = 0
        ptEnd_col = width
        ptStart= (position,ptStart_col)
        ptEnd = (position,ptEnd_col)
    else:
        ptStart_row = 0
        prEnd_row = height
        ptStart= (ptStart_col,position)
        ptEnd = (ptEnd_col,position)
    if REF_or_TAG == 0:
        thickness = 1
        color = (0,255,0)
        lineType = 4
    else:
        thickness = 3
        color = (0,0,255)
        lineType = 8
    new_image = cv2.line(image,ptStart,ptEnd,color,thickness,lineType)
    return new_image
        
def draw_REF_lines(image):
    shape = image.shape
    height = shape[0]
    width = shape[1]
    deci_width = int(ceil(width/10))
    deci_height = int(ceil(height/10))
    REF_width_positions = [
        deci_width*1,deci_width*2,deci_width*3,deci_width*4,deci_width*5,
        deci_width*6,deci_width*7,deci_width*8,deci_width*9
        ]
    REF_height_positions = [
        deci_height*1,deci_height*2,deci_height*3,deci_height*4,deci_height*5,
        deci_height*6,deci_height*7,deci_height*8,deci_height*9
        ]
    for position in REF_width_positions:
        draw_line(image,position,ROW,REF)
    for position in REF_height_positions:
        draw_line(image,position,COL,REF)
        draw_tag(image,str(REF_height_positions.index(position)+1),ceil(position - deci_height/2 + 11),10)
    draw_tag(image,'0',ceil(deci_height*10 - deci_height/2 + 11),10)
    for index in range(0,10):
        if index == 0:
            tag = 'Q'
        elif index == 1:
            tag = 'W'
        elif index == 2:
            tag = 'E'
        elif index == 3:
            tag = 'R'
        elif index == 4:
            tag = 'T'
        elif index == 5:
            tag = 'Y'
        elif index == 6:
            tag = 'U'
        elif index == 7:
            tag = 'I'
        elif index == 8:
            tag = 'O'
        elif index == 9:
            tag = 'P'
        draw_tag(image,tag,30,ceil(index*deci_width + deci_width/2 -11))

    return

def get_position_by_num(image,num):
    shape = image.shape
    height = shape[0]
    width = shape[1]
    deci_width = int(ceil(width/10))
    deci_height = int(ceil(height/10))
    REF_width_positions = [
        deci_width*1,deci_width*2,deci_width*3,deci_width*4,deci_width*5,
        deci_width*6,deci_width*7,deci_width*8,deci_width*9,deci_width*10,
        ]
    REF_height_positions = [
        deci_height*1,deci_height*2,deci_height*3,deci_height*4,deci_height*5,
        deci_height*6,deci_height*7,deci_height*8,deci_height*9,deci_height*10,
        ]
    if num == 1:
        position_width = 0 + ceil(REF_width_positions[0]/2)
        position_height = 0 + ceil(REF_height_positions[0]/2)
    elif num == 0:
        position_width = REF_width_positions[10-2] + ceil((REF_width_positions[10-1]-REF_width_positions[10-2])/2)
        position_height = REF_height_positions[10-2] + ceil((REF_height_positions[10-1]-REF_height_positions[10-2])/2)
    else:
        position_width = REF_width_positions[num-2] + ceil((REF_width_positions[num-1]-REF_width_positions[num-2])/2)
        position_height = REF_height_positions[num-2] + ceil((REF_height_positions[num-1]-REF_height_positions[num-2])/2)
    return position_height,position_width

def move_line_num(source_img,num_key):
        global positionRow
        try:
            if not pressed_key[-1].isalpha():
                img_BGRA = cv2.merge((b_channel,g_channel,r_channel,alpha_channel))
                draw_REF_lines(img_BGRA)
        except:
            pass
        pressed_key.append(num_key)
        positionRow,_positionCol = get_position_by_num(img_BGRA,1)
        draw_line(source_img,positionRow,COL,TAG)
        cv2.imshow(title, source_img)

        return

ROW,COL = 0,1
REF,TAG = 0,1
left = 20
top = 20
right = 400
bottom = 400
color = 'green'
#cv3.imshow(title, img_BGRA)
tag = 'test'
position_row_tag=100
position_col_tag=100
#positionCol = 0
#positionRow = 0
while True:

#draw_tag(img_BGRA,tag,position_row_tag,position_col_tag)
#draw_line(img_BGRA,position_row_tag,ROW,REF)
    draw_REF_lines(img_BGRA)
#draw_box(img_BGRA,left,top,right,bottom,color)
    cv2.imshow(title, img_BGRA)
    wait_key = cv2.waitKeyEx(1)
    if wait_key == 27:
        cv2.destroyAllWindows()
        sys.exit()
    elif wait_key == 49:# 1
        try:
            if not pressed_key[-1].isalpha():
                img_BGRA = cv2.merge((b_channel,g_channel,r_channel,alpha_channel))
                draw_REF_lines(img_BGRA)
        except:
            pass
        pressed_key.append('1')
        positionRow,_positionCol = get_position_by_num(img_BGRA,1)
        draw_line(img_BGRA,positionRow,COL,TAG)
        cv2.imshow(title, img_BGRA)
    elif wait_key == 50:# 2
        try:
            if not pressed_key[-1].isalpha():
                img_BGRA = cv2.merge((b_channel,g_channel,r_channel,alpha_channel))
                draw_REF_lines(img_BGRA)
        except:
            pass
        pressed_key.append('2')
        positionRow,_positionCol = get_position_by_num(img_BGRA,2)
        draw_line(img_BGRA,positionRow,COL,TAG)
        cv2.imshow(title, img_BGRA)
    elif wait_key == 51:# 3
        try:
            if not pressed_key[-1].isalpha():
                img_BGRA = cv2.merge((b_channel,g_channel,r_channel,alpha_channel))
                draw_REF_lines(img_BGRA)
        except:
            pass
        pressed_key.append('3')
        positionRow,_positionCol = get_position_by_num(img_BGRA,3)
        draw_line(img_BGRA,positionRow,COL,TAG)
        cv2.imshow(title, img_BGRA)
    elif wait_key == 52:# 4
        try:
            if not pressed_key[-1].isalpha():
                img_BGRA = cv2.merge((b_channel,g_channel,r_channel,alpha_channel))
                draw_REF_lines(img_BGRA)
        except:
            pass
        pressed_key.append('4')
        positionRow,_positionCol = get_position_by_num(img_BGRA,4)
        draw_line(img_BGRA,positionRow,COL,TAG)
        cv2.imshow(title, img_BGRA)
    elif wait_key == 53:# 5
        try:
            if not pressed_key[-1].isalpha():
                img_BGRA = cv2.merge((b_channel,g_channel,r_channel,alpha_channel))
                draw_REF_lines(img_BGRA)
        except:
            pass
        pressed_key.append('5')
        positionRow,_positionCol = get_position_by_num(img_BGRA,5)
        draw_line(img_BGRA,positionRow,COL,TAG)
        cv2.imshow(title, img_BGRA)
    elif wait_key == 54:# 6
        try:
            if not pressed_key[-1].isalpha():
                img_BGRA = cv2.merge((b_channel,g_channel,r_channel,alpha_channel))
                draw_REF_lines(img_BGRA)
        except:
            pass
        pressed_key.append('6')
        positionRow,_positionCol = get_position_by_num(img_BGRA,6)
        draw_line(img_BGRA,positionRow,COL,TAG)
        cv2.imshow(title, img_BGRA)
    elif wait_key == 55:# 7
        try:
            if not pressed_key[-1].isalpha():
                img_BGRA = cv2.merge((b_channel,g_channel,r_channel,alpha_channel))
                draw_REF_lines(img_BGRA)
        except:
            pass
        pressed_key.append('7')
        positionRow,_positionCol = get_position_by_num(img_BGRA,7)
        draw_line(img_BGRA,positionRow,COL,TAG)
        cv2.imshow(title, img_BGRA)
    elif wait_key == 56:# 8
        try:
            if not pressed_key[-1].isalpha():
                img_BGRA = cv2.merge((b_channel,g_channel,r_channel,alpha_channel))
                draw_REF_lines(img_BGRA)
        except:
            pass
        pressed_key.append('8')
        positionRow,_positionCol = get_position_by_num(img_BGRA,8)
        draw_line(img_BGRA,positionRow,COL,TAG)
        cv2.imshow(title, img_BGRA)
    elif wait_key == 57:# 9
        try:
            if not pressed_key[-1].isalpha():
                img_BGRA = cv2.merge((b_channel,g_channel,r_channel,alpha_channel))
                draw_REF_lines(img_BGRA)
        except:
            pass
        pressed_key.append('9')
        positionRow,_positionCol = get_position_by_num(img_BGRA,9)
        draw_line(img_BGRA,positionRow,COL,TAG)
        cv2.imshow(title, img_BGRA)
    elif wait_key == 48:# 0
        try:
            if not pressed_key[-1].isalpha():
                img_BGRA = cv2.merge((b_channel,g_channel,r_channel,alpha_channel))
                draw_REF_lines(img_BGRA)
        except:
            pass
        pressed_key.append('0')
        positionRow,_positionCol = get_position_by_num(img_BGRA,0)
        draw_line(img_BGRA,positionRow,COL,TAG)
        cv2.imshow(title, img_BGRA)
    elif wait_key == 113:# q
        try:
            if not pressed_key[-1].isdigit():
                img_BGRA = cv2.merge((b_channel,g_channel,r_channel,alpha_channel))
                draw_REF_lines(img_BGRA)
        except:
            pass
        pressed_key.append('q')
        _positionRow,positionCol = get_position_by_num(img_BGRA,1)
        draw_line(img_BGRA,positionCol,ROW,TAG)
        cv2.imshow(title, img_BGRA)
    elif wait_key == 119:# w
        try:
            if not pressed_key[-1].isdigit():
                img_BGRA = cv2.merge((b_channel,g_channel,r_channel,alpha_channel))
                draw_REF_lines(img_BGRA)
        except:
            pass
        pressed_key.append('w')
        _positionRow,positionCol = get_position_by_num(img_BGRA,2)
        draw_line(img_BGRA,positionCol,ROW,TAG)
        cv2.imshow(title, img_BGRA)
    elif wait_key == 101:# e
        try:
            if not pressed_key[-1].isdigit():
                img_BGRA = cv2.merge((b_channel,g_channel,r_channel,alpha_channel))
                draw_REF_lines(img_BGRA)
        except:
            pass
        pressed_key.append('e')
        _positionRow,positionCol = get_position_by_num(img_BGRA,3)
        draw_line(img_BGRA,positionCol,ROW,TAG)
        cv2.imshow(title, img_BGRA)
    elif wait_key == 114:# r
        try:
            if not pressed_key[-1].isdigit():
                img_BGRA = cv2.merge((b_channel,g_channel,r_channel,alpha_channel))
                draw_REF_lines(img_BGRA)
        except:
            pass
        pressed_key.append('r')
        _positionRow,positionCol = get_position_by_num(img_BGRA,4)
        draw_line(img_BGRA,positionCol,ROW,TAG)
        cv2.imshow(title, img_BGRA)
    elif wait_key == 116:# t
        try:
            if not pressed_key[-1].isdigit():
                img_BGRA = cv2.merge((b_channel,g_channel,r_channel,alpha_channel))
                draw_REF_lines(img_BGRA)
        except:
            pass
        pressed_key.append('t')
        _positionRow,positionCol = get_position_by_num(img_BGRA,5)
        draw_line(img_BGRA,positionCol,ROW,TAG)
        cv2.imshow(title, img_BGRA)
    elif wait_key == 121:# y
        try:
            if not pressed_key[-1].isdigit():
                img_BGRA = cv2.merge((b_channel,g_channel,r_channel,alpha_channel))
                draw_REF_lines(img_BGRA)
        except:
            pass
        pressed_key.append('y')
        _positionRow,positionCol = get_position_by_num(img_BGRA,6)
        draw_line(img_BGRA,positionCol,ROW,TAG)
        cv2.imshow(title, img_BGRA)
    elif wait_key == 117:# u
        try:
            if not pressed_key[-1].isdigit():
                img_BGRA = cv2.merge((b_channel,g_channel,r_channel,alpha_channel))
                draw_REF_lines(img_BGRA)
        except:
            pass
        pressed_key.append('u')
        _positionRow,positionCol = get_position_by_num(img_BGRA,7)
        draw_line(img_BGRA,positionCol,ROW,TAG)
        cv2.imshow(title, img_BGRA)
    elif wait_key == 105:# i
        try:
            if not pressed_key[-1].isdigit():
                img_BGRA = cv2.merge((b_channel,g_channel,r_channel,alpha_channel))
                draw_REF_lines(img_BGRA)
        except:
            pass
        pressed_key.append('i')
        _positionRow,positionCol = get_position_by_num(img_BGRA,8)
        draw_line(img_BGRA,positionCol,ROW,TAG)
        cv2.imshow(title, img_BGRA)
    elif wait_key == 111:# o
        try:
            if not pressed_key[-1].isdigit():
                img_BGRA = cv2.merge((b_channel,g_channel,r_channel,alpha_channel))
                draw_REF_lines(img_BGRA)
        except:
            pass
        pressed_key.append('o')
        _positionRow,positionCol = get_position_by_num(img_BGRA,9)
        draw_line(img_BGRA,positionCol,ROW,TAG)
        cv2.imshow(title, img_BGRA)
    elif wait_key == 112:# p
        try:
            if not pressed_key[-1].isdigit():
                img_BGRA = cv2.merge((b_channel,g_channel,r_channel,alpha_channel))
                draw_REF_lines(img_BGRA)
        except:
            pass
        pressed_key.append('p')
        _positionRow,positionCol = get_position_by_num(img_BGRA,0)
        draw_line(img_BGRA,positionCol,ROW,TAG)
        cv2.imshow(title, img_BGRA)
    elif wait_key == 100:# d
        try:
            if not pressed_key[-1].isdigit():
                img_BGRA = cv2.merge((b_channel,g_channel,r_channel,alpha_channel))
                draw_REF_lines(img_BGRA)
        except:
            pass
        pressed_key.append('d')
        positionCol -= 10
        draw_line(img_BGRA,positionCol,ROW,TAG)
        cv2.imshow(title, img_BGRA)
    elif wait_key == 102:# f
        try:
            if not pressed_key[-1].isdigit():
                img_BGRA = cv2.merge((b_channel,g_channel,r_channel,alpha_channel))
                draw_REF_lines(img_BGRA)
        except:
            pass
        pressed_key.append('f')
        positionCol += 10
        draw_line(img_BGRA,positionCol,ROW,TAG)
        cv2.imshow(title, img_BGRA)
    elif wait_key == 106:# j
        try:
            if not pressed_key[-1].isdigit():
                img_BGRA = cv2.merge((b_channel,g_channel,r_channel,alpha_channel))
                draw_REF_lines(img_BGRA)
        except:
            pass
        pressed_key.append('j')
        positionRow += 10
        draw_line(img_BGRA,positionRow,COL,TAG)
        cv2.imshow(title, img_BGRA)
    elif wait_key == 107:# k
        try:
            if not pressed_key[-1].isdigit():
                img_BGRA = cv2.merge((b_channel,g_channel,r_channel,alpha_channel))
                draw_REF_lines(img_BGRA)
        except:
            pass
        pressed_key.append('k')
        positionRow -= 10
        draw_line(img_BGRA,positionRow,COL,TAG)
        cv2.imshow(title, img_BGRA)
#cv2.waitKey(0)
